package sfeir.ergosum;

import sfeir.ergosum.tools.ThemeColor;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

import com.sfeir.proto.hivelayout.HiveAction;
import com.sfeir.proto.hivelayout.HiveActionCell;
import com.sfeir.proto.hivelayout.HiveDialView;

public class DialActivity extends Activity {

    HiveDialView hdv;

    HiveAction action0 = new HiveAction("0") {
        public void executeAction() {
            hdv.append("0");
        };
    };

    HiveAction action1 = new HiveAction("1") {
        public void executeAction() {
            hdv.append("1");
        };
    };

    HiveAction action2 = new HiveAction("2") {
        public void executeAction() {
            hdv.append("2");
        };
    };

    HiveAction action3 = new HiveAction("3") {
        public void executeAction() {
            hdv.append("3");
        };
    };

    HiveAction action4 = new HiveAction("4") {
        public void executeAction() {
            hdv.append("4");
        };
    };

    HiveAction action5 = new HiveAction("5") {
        public void executeAction() {
            hdv.append("5");
        };
    };

    HiveAction action6 = new HiveAction("6") {
        public void executeAction() {
            hdv.append("6");
        };
    };

    HiveAction action7 = new HiveAction("7") {
        public void executeAction() {
            hdv.append("7");
        };
    };

    HiveAction action8 = new HiveAction("8") {
        public void executeAction() {
            hdv.append("8");
        };
    };

    HiveAction action9 = new HiveAction("9") {
        public void executeAction() {
            hdv.append("9");
        };
    };

    HiveAction actionTel = new HiveAction("Tel") {
        public void executeAction() {
            Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                    + hdv.getNumber().getText()));
            DialActivity.this.startActivity(i);
        };
    };

    HiveAction actionSms = new HiveAction("Sms") {
        public void executeAction() {
            Intent iSms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
                    + hdv.getNumber().getText()));
            DialActivity.this.startActivity(iSms);
        };
    };

    HiveAction actionBack = new HiveAction("<-") {
        public void executeAction() {
            hdv.back();
        };
    };

    HiveAction actionStar = new HiveAction("*") {
        public void executeAction() {
            hdv.append("*");
        };
    };

    HiveAction actionSharp = new HiveAction("#") {
        public void executeAction() {
            hdv.append("#");
        };
    };

    HiveAction actionPlus = new HiveAction("+") {
        public void executeAction() {
            hdv.append("+");
        };
    };

    HiveAction actionClear = new HiveAction("Clear") {
        public void executeAction() {
            hdv.clear();
        };
    };

    HiveAction actionCon = new HiveAction("Contacts") {
        public void executeAction() {
            HiveActivity.startActivity(getApplicationContext(),"" ,"");
            finish();
        };
    };

    HiveAction actionLog = new HiveAction("Log") {
        public void executeAction() {
            finish();
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        int height = getWindow().getWindowManager().getDefaultDisplay()
                .getHeight();
        int width = getWindow().getWindowManager().getDefaultDisplay()
                .getWidth();

        hdv = new HiveDialView(this, height, width);
        hdv.setBackgroundColor(0x00000000);

        ThemeColor theme = new ThemeColor(DialActivity.this);
        int color1 = theme.getCellColors().get(0);
        int color2 = theme.getCellColors().get(1);
        int color3 = theme.getCellColors().get(2);
        int color4 = theme.getMenuColors().get(0);

        hdv.getNumber().setTextColor(0xFFFFFFFF);
        
        HiveActionCell cs = new HiveActionCell(this, action0);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial0(cs);

        cs = new HiveActionCell(this, action1);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial1(cs);

        cs = new HiveActionCell(this, action2);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial2(cs);

        cs = new HiveActionCell(this, action3);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial3(cs);

        cs = new HiveActionCell(this, action4);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial4(cs);

        cs = new HiveActionCell(this, action5);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial5(cs);

        cs = new HiveActionCell(this, action6);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial6(cs);

        cs = new HiveActionCell(this, action7);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial7(cs);

        cs = new HiveActionCell(this, action8);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial8(cs);

        cs = new HiveActionCell(this, action9);
        cs.setCellAlpha(255);
        cs.setCellColor(color1);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDial9(cs);

        cs = new HiveActionCell(this, actionTel);
        cs.setCellAlpha(255);
        cs.setCellColor(color2);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialTel(cs);

        cs = new HiveActionCell(this, actionSms);
        cs.setCellAlpha(255);
        cs.setCellColor(color2);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialSms(cs);

        cs = new HiveActionCell(this, actionBack);
        cs.setCellAlpha(255);
        cs.setCellColor(color3);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialBack(cs);

        cs = new HiveActionCell(this, actionStar);
        cs.setCellAlpha(255);
        cs.setCellColor(color4);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialStar(cs);

        cs = new HiveActionCell(this, actionSharp);
        cs.setCellAlpha(255);
        cs.setCellColor(color4);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialSharp(cs);

        cs = new HiveActionCell(this, actionPlus);
        cs.setCellAlpha(255);
        cs.setCellColor(color4);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialPlus(cs);

        cs = new HiveActionCell(this, actionCon);
        cs.setCellAlpha(255);
        cs.setCellColor(color2);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialCon(cs);

        cs = new HiveActionCell(this, actionLog);
        cs.setCellAlpha(255);
        cs.setCellColor(color2);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialLog(cs);

        cs = new HiveActionCell(this, actionClear);
        cs.setCellAlpha(255);
        cs.setCellColor(color4);
        cs.setShown(true);
        cs.setEmboss(true);

        hdv.setHiveDialClear(cs);

        hdv.setup();
        setContentView(hdv);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 5) {
            Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                    + hdv.getNumber().getText()));
            DialActivity.this.startActivity(i);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }

}
